using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
namespace LdExpress
{
    public class CustomerActionPlan
    {
        public long ID = -1;
        public string ContactDescription = "";
        public string ActionPlan = "";
        public long CustRelID = 0;
        public int Reminder = 0;
        public long Owner = 0;
        public DateTime LastUpdate = DateTime.Now;

        public CustomerActionPlan()
        {
        }

        public CustomerActionPlan(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_customeractionplan";

            if (ID <= 0) // new object, we insert new record to database
            {
                strSQL = "Insert into " + strTableName + " (" +
                            "`ContactDescription`" +
                            ",`ActionPlan`" +
                            ",`LastUpdate`" +
                            ",`CustRelID`" +
                            ",`Owner`" +
                            ",`Reminder`" +
                            ") " +
                         " values(" +
                             "'" + DBModule.RefineString(ContactDescription) + "'" +
                             ",'" + DBModule.RefineString(ActionPlan) + "'" +
                             "," + DBModule.RefineDatetime(LastUpdate, true) +
                             "," + CustRelID.ToString() +
                             "," + Owner.ToString() +
                             "," + Reminder.ToString() +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("customeractionplan", "add", cn, trans);
            }
            else // edit object
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "ContactDescription='" + DBModule.RefineString(ContactDescription) + "'" +
                            ",ActionPlan='" + DBModule.RefineString(ActionPlan) + "'" +
                            ",LastUpdate=" + DBModule.RefineDatetime(LastUpdate, true) + "" +
                            ",CustRelID = " + CustRelID.ToString() +
                            ",Owner = " + Owner.ToString() +
                            ",Reminder = " + Reminder.ToString() +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("customeractionplan", "edit", cn, trans);
            }                                    
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_customeractionplan WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("customeractionplan", "delete", cn, trans);
        }
        public void Delete(long CustRelID, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_customeractionplan WHERE CustRelID = " + CustRelID;
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_customeractionplan WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("ContactDescription")) ContactDescription = dr["ContactDescription"].ToString();
                if (!dr.IsNull("ActionPlan")) ActionPlan = dr["ActionPlan"].ToString();
                if (!dr.IsNull("LastUpdate")) LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
                if (!dr.IsNull("CustRelID")) CustRelID = long.Parse(dr["CustRelID"].ToString());
                if (!dr.IsNull("Owner")) Owner = long.Parse(dr["Owner"].ToString());
                if (!dr.IsNull("Reminder")) Reminder = int.Parse(dr["Reminder"].ToString());
            }
        }
        public void GetList(string strCondition, string iDs, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_customeractionplan where 1=1 ";
            if (iDs != "")
                strSQL += " AND ID=" + iDs;
            if ((strCondition != null) && (strCondition != ""))
            {
                if (strCondition != "All") strSQL += strCondition;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL += " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }

        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_customerofferprices where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
        public static bool HasActionPlan(long custID, MySqlConnection cn, MySqlTransaction trans)
        {
            bool retVal = false;
            string strSQL = "";
            DataSet ds = null;
            strSQL = "SELECT `ActionPlan`,`LastUpdate` FROM `tbl_customeractionplan` WHERE custRelID=" + custID + " ORDER BY lastUpdate DESC LIMIT 0,1 ";
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (DateTime.Parse(ds.Tables[0].Rows[0]["LastUpdate"].ToString()).AddHours(10) >= DateTime.Now && ds.Tables[0].Rows[0]["ActionPlan"].ToString() != "")
                {
                    retVal = true;
                }

            }
            return retVal;
        }
    }
}


